package com.zhanggm.springcloud.jpa.dao;

import com.zhanggm.springcloud.jpa.entity.UserEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface UserRepositroy extends JpaRepository<UserEntity,Long>, JpaSpecificationExecutor<UserEntity> {
    //按用户名和密码查询用户
    List<UserEntity> findByUsernameAndPassword(String username,String password);
    //按用户名模糊查询
    List<UserEntity> findByUsernameLike(String username);
    //分页排序
    Page<UserEntity> findByUsernameLike(String username, Pageable pageable);
    //多条件查询
    @Query("select u from UserEntity u where u.username=?1 and u.password=?2")
    List<UserEntity> getByUsernameAndPassword(String username,String password);

    @Query("select u from UserEntity u where u.username=?1 and u.password=?2")
    Page<UserEntity> getByUsernameAndPassword(String username,String password,Pageable pageable);

    @Query(value = "select u.* from t_user u where u.username=?1 and u.password=?2",nativeQuery = true)
    Page<UserEntity> getByUnameAndPwd(String username,String password,Pageable pageable);


    @Query("select u from UserEntity u where u.username=:uname and u.password=:pwd")
    List<UserEntity> getByUsernameAndPasswordParam(@Param("uname") String username, @Param("pwd") String password);

    @Modifying
    @Transactional
    @Query("update UserEntity u set u.username=?2 where id=?1")
    int updateUsernameById(Long id,String username);
}
